<?php
namespace app\admin\controller;
use think\Db;
use think\Request;
class Order extends Base
{
    /**
     * 订单数据-
     */
    public function index(){
        if(request()->isAjax ()){
            extract(input());
            $map = [];
            $map['o.order_delete'] = 1;
            $authSta = Db::table('up_auth_group')->where ('id' , session('agid'))->value ('auth_status');
            if($authSta == 2){
                $map['o.order_admin_id'] = session('uid');
            }
            if(isset($n_title)&&$n_title!="")
            {
                $map['order_username|order_phone|order_company'] = ['like',"%" . $n_title . "%"];
            }
            if(isset($order_no)&&$order_no!="")
            {
                $map['order_no'] = ['like',"%" . $order_no . "%"];
            }
            if(isset($order_status)&&$order_status!="")
            {
                $map['order_status'] = $order_status;
            }
            if(isset($order_jiance_status)&&$order_jiance_status!="")
            {
                $map['order_jiance_status'] = $order_jiance_status;
            }
            if(isset($start)&&$start!=""&&isset($end)&&$end=="")
            {
                $map['order_time'] = ['>= time',$start];
            }
            if(isset($end)&&$end!=""&&isset($start)&&$start=="")
            {
                $map['order_time'] = ['<= time',$end];
            }
            if(isset($start)&&$start!=""&&isset($end)&&$end!="")
            {
                $map['order_time'] = ['between time',[$start,$end]];
            }

            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $field = 'o.*,user_username,user_phone';
            $count = Db::table('up_order')->alias('o')
                ->join('up_u_user u','o.order_user_id=u.user_id','LEFT')
                ->where($map)
                ->count();// 获取总条数
            $lists = Db::table('up_order')->alias('o')
                ->join('up_u_user u','o.order_user_id=u.user_id','LEFT')
                ->where($map)
                ->order('order_time DESC')
                ->page($Nowpage,$limits)
                ->field($field)
                ->select();
            if($lists){
                foreach ($lists as $k => $v){
                    $lists[$k]['baogaoNum'] = Db::table('up_order_info')
                        ->where('oi_order_id',$v['order_id'])
                        ->where('oi_wx_report_url','not null')
                        ->where('oi_wx_report_url','<>','')
                        ->count();
                    $xuliehao = Db::table('up_order_info')
                        ->where('oi_order_id',$v['order_id'])
                        ->column('oi_xulie');
                    $lists[$k]['xuliehao'] = implode('，',$xuliehao,);
                }
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $xilies = Db::table('up_xl')
            ->where('device_pid',0)
            ->where('device_status',1)
            ->field('device_id,device_name')
            ->select();
        $this->assign('xilies',$xilies);
        return $this->fetch('order/index');
    }
    /**
     * 订单详情
     */
    public function orderInfo(){
        $order_id = input('order_id');
        $order = Db::table('up_order')
            ->where('order_id',$order_id)
            ->find();
        $where = [
            'oi_order_id' => $order_id
        ];
        $field = 'od.*,xl1.device_name xlname,xl2.device_name xhname,gz_name';
        $details = Db::table('up_order_info')->alias('od')
            ->join("up_xl xl1",'od.oi_xl=xl1.device_id','LEFT')
            ->join("up_xl xl2",'od.oi_xh=xl2.device_id','LEFT')
            ->join("up_gz gz",'od.oi_gz=gz.gz_id','LEFT')
            ->where($where)
            ->field($field)
            ->select();
        foreach ($details as $k => $v){
            $details[$k]['imgs'] = [];
            if($v['oi_front_image']) $details[$k]['imgs'][] = $v['oi_front_image'];
            if($v['oi_side_image']) $details[$k]['imgs'][] = $v['oi_side_image'];
            if($v['oi_mp_image']) $details[$k]['imgs'][] = $v['oi_mp_image'];

            if($v['oi_other_image']){
                $details[$k]['oi_other_image'] = explode(',',$v['oi_other_image']);
            }else{
                $details[$k]['oi_other_image'] = [];
            }
            if(!$v['oi_other_video']) $details[$k]['oi_other_video'] = '';
        }
        $orderStatus = ['','待审核','已同意(待寄回)','已拒绝','返厂中(用户已寄回)','检修中','已回寄给用户','已完成','已取消'];
        $orderJcStatus = ['--','待审核','已同意','已拒绝'];
        $huijiTypes = ['暂未指定','包装出货','入CUS库','入RMASAC库'];
        $order['order_admin_name'] = '';
        if($order['order_admin_id']) {
            $order['order_admin_name'] = Db::table('up_admin')->where ('id' , $order['order_admin_id'])->value('real_name');
        }
        $array = $this->arrayEmpty;
        $express2 = $order['order_nj_user2'] ? json_decode($order['order_nj_user2'],true) : $array;
        $express3 = $order['order_nj_user3'] ? json_decode($order['order_nj_user3'],true) : $array;
        return $this->fetch('order/orderinfo',['data'=>$order,'goods'=>$details
            ,'orderStatus'=>$orderStatus
            ,'orderJcStatus'=>$orderJcStatus
            ,'huijiTypes'=>$huijiTypes
            ,'express2'=>$express2
            ,'express3'=>$express3
        ]);
    }
    /**
     * 审核
     */
    public function orderCheck(){
        extract(input());
        if(Request::instance()->isPost()){
            $param  = [
                'order_status'=>$order_status,
                'order_check_time'=>date('Y-m-d H:i:s'),
            ];
            if($order_status == 3 && $order_refuse_reason) $param['order_refuse_reason'] = $order_refuse_reason;
            if($order_status == 2){
                $param['order_jiance_status'] = 1;
                if(!$order_company_addr_id) return json(['code'=>100,'msg'=>'请选择收货地址']);
                $address = Db::table('up_address')
                    ->where('addr_id',$order_company_addr_id)
                    ->find();
                $param['order_company_addr_id'] = $order_company_addr_id;
                $param['order_company_name'] = $address['addr_username'];
                $param['order_company_phone'] = $address['addr_phone'];
                $param['order_company_address'] = $address['addr_address'];
            }
            $res = Db::table('up_order')
                ->where('order_id',$order_id)
                ->update($param);
            if($res){
                if($order_status == 2){
                    try{
                        $phone = Db::table('up_order')->where('order_id',$order_id)->value('order_phone');
                        $templateid = config('yzx.templateid3');
                        YzxSms('已同意', $phone, $templateid);
                    }catch (\Exception $e){}
                }
                return json(['code'=>200,'msg'=>'修改成功']);
            }else{
                return json(['code'=>100,'msg'=>'修改失败']);
            }
        }
        $order = Db::table('up_order')
            ->where('order_id',$order_id)
            ->find();
        $address = Db::table('up_address')
            ->where('addr_type',2)
            ->where('addr_status',1)
            ->select();
        return $this->fetch('order/check',['data'=>$order,'address'=>$address]);
    }
    /**
     * 审核-检测
     */
    public function orderJianceCheck(){
        extract(input());
        if(Request::instance()->isPost()){
            $param  = [
                'order_jiance_status'=>$order_jiance_status,
                'order_jiance_time'=>date('Y-m-d H:i:s'),
            ];
            if($order_jiance_status == 3 && !$order_jiance_remark) return json(['code'=>100,'msg'=>'请填写拒绝原因']);
            if($order_jiance_remark) $param['order_jiance_remark'] = $order_jiance_remark;
            $res = Db::table('up_order')
                ->where('order_id',$order_id)
                ->update($param);
            if($res){
                return json(['code'=>200,'msg'=>'操作成功']);
            }else{
                return json(['code'=>100,'msg'=>'操作失败']);
            }
        }
        $order = Db::table('up_order')
            ->where('order_id',$order_id)
            ->find();
        return $this->fetch('order/checkjc',['data'=>$order]);
    }
    /**
     * 维修方式
     */
    public function orderWxfs(){
        extract(input());
        if(Request::instance()->isPost()){
            if(!$order_wxfs_id) return json(['code'=>100,'msg'=>'请选择']);
            $param  = [
                'order_wxfs_id'=>$order_wxfs_id
            ];
            $order_wxfs_name = Db::table('up_wxfs')
                ->where('gz_id',$order_wxfs_id)
                ->value('gz_name');
            $param['order_wxfs_name'] = $order_wxfs_name;
            $res = Db::table('up_order')
                ->where('order_id',$order_id)
                ->update($param);
            if($res){
                return json(['code'=>200,'msg'=>'操作成功']);
            }else{
                return json(['code'=>100,'msg'=>'操作失败']);
            }
        }
        $order = Db::table('up_order')
            ->where('order_id',$order_id)
            ->find();
        $datas = Db::table('up_wxfs')
            ->where('gz_status',1)
            ->select();
        return $this->fetch('order/wxfs',['data'=>$order,'datas'=>$datas]);
    }
    /**
     * 订单分配
     */
    public function orderAdmin(){
        extract(input());
        if(Request::instance()->isPost()){
            if(!$order_admin_id) return json(['code'=>100,'msg'=>'请选择']);
            $param  = [
                'order_admin_id' => $order_admin_id
            ];
            $res = Db::table('up_order')
                ->where('order_id',$order_id)
                ->update($param);
            try{
                $name = Db::table('up_order')->where('order_id',$order_id)->value('order_company');
                $phone = Db::table('up_admin')->where('id',$order_admin_id)->value('phone');
                $templateid = config('yzx.templateid2');
                YzxSms($name, $phone, $templateid);
            }catch (\Exception $e){}
            return json(['code'=>200,'msg'=>'操作成功']);
        }
        $order = Db::table('up_order')
            ->where('order_id',$order_id)
            ->find();
        $datas = Db::name('admin')->alias('a')
            ->join('auth_group ag','a.groupid=ag.id','left')
            ->where('a.status', 1)
            ->where('ag.auth_status', 2)
            ->field('a.id,a.username,a.real_name,ag.title')
            ->select();
        return $this->fetch('order/admin',['data'=>$order,'datas'=>$datas]);
    }
    /**
     * 修改收货地址
     */
    public function orderUpdateAddress(){
        extract(input());
        if(Request::instance()->isPost()){
            if(!$order_company_addr_id) return json(['code'=>100,'msg'=>'请选择收货地址']);
            $address = Db::table('up_address')
                ->where('addr_id',$order_company_addr_id)
                ->find();
            $param = [];
            $param['order_company_addr_id'] = $order_company_addr_id;
            $param['order_company_name'] = $address['addr_username'];
            $param['order_company_phone'] = $address['addr_phone'];
            $param['order_company_address'] = $address['addr_address'];
            $res = Db::table('up_order')
                ->where('order_id',$order_id)
                ->update($param);
            return json(['code'=>200,'msg'=>'修改成功']);
        }
        $order = Db::table('up_order')
            ->where('order_id',$order_id)
            ->find();
        $address = Db::table('up_address')
            ->where('addr_type',2)
            ->where('addr_status',1)
            ->select();
        return $this->fetch('order/orderupdateaddress',['data'=>$order,'address'=>$address]);
    }
    /**
     * 确认收到订单
     * @return \think\response\Json
     */
    public function orderSureGet(){
        $order_id = input('id');
        $now_status = Db::table('up_order')->where('order_id',$order_id)->value('order_status');
        if($now_status != 4) return json(['code' => 100, 'msg' => '只有待确认收货的才可以操作']);
        $res = Db::table('up_order')
            ->where('order_id',$order_id)
            ->update([
                'order_status' => 5,
                'order_company_shou_time' => date('Y-m-d H:i:s')
            ]);
        if($res){
            return json(['code' => 200, 'msg' => '确认成功']);
        }else{
            return json(['code' => 100, 'msg' => '确认失败']);
        }
    }
    /**
     * 订单发货
     */
    public function orderSend(){
        extract(input());
        if(Request::instance()->isPost()){
            if(!isset($order_company_express_type) || !$order_company_express_type) return json(['code' => 100, 'msg' => '请选择回寄用户的方式']);
            $now_status = Db::table('up_order')->where('order_id',$order_id)->value('order_status');
            Db::startTrans();
            try{
                $updateExpress = [];
                $today = date('Y-m-d H:i:s');
                $update = [
                    'order_company_express_type' => $order_company_express_type
                ];
                if($type == 1){
                    if($now_status != 5) return json(['code' => 100, 'msg' => '只有待回寄的才可以操作']);
                    $update['order_status'] = 6;
                    $update['order_company_send_time'] = $today;
                }
                if($type == 3){
                    $update['order_status'] = 6;
                    $update['order_company_send_time'] = $today;
                }
                if($order_company_express_type == 1){
                    $updateExpress = [
                        'order_user_name' => $order_user_name,
                        'order_user_phone' => $order_user_phone,
                        'order_user_address' => $order_user_address,
                        'order_company_express_company' => $order_company_express_company,
                        'order_company_express_code' => $order_company_express_code,
                        'order_company_express_nums' => $order_company_express_nums
                    ];
                    if(!$order_user_name || !$order_user_phone || !$order_user_address) return json(['code' => 100, 'msg' => '请填写收件人信息']);
//                    if(!$order_company_express_company || !$order_company_express_code || !$order_company_express_nums) return json(['code' => 100, 'msg' => '请填写物流信息']);
                    $updateExpress2 = [
                        'order_user_name' => $order_user_name2,
                        'order_user_phone' => $order_user_phone2,
                        'order_user_address' => $order_user_address2,
                        'order_company_express_company' => $order_company_express_company2,
                        'order_company_express_code' => $order_company_express_code2,
                        'order_company_express_nums' => $order_company_express_nums2
                    ];
                    $updateExpress3 = [
                        'order_user_name' => $order_user_name3,
                        'order_user_phone' => $order_user_phone3,
                        'order_user_address' => $order_user_address3,
                        'order_company_express_company' => $order_company_express_company3,
                        'order_company_express_code' => $order_company_express_code3,
                        'order_company_express_nums' => $order_company_express_nums3
                    ];
                    $update['order_nj_user2'] = json_encode($updateExpress2);
                    $update['order_nj_user3'] = json_encode($updateExpress3);
                }
                if($updateExpress) $update = array_merge($update,$updateExpress);
                Db::table('up_order')
                    ->where('order_id',$order_id)
                    ->update($update);
                Db::commit();
                return json(['code' => 200, 'msg' => '成功']);
            }catch (\Exception $e) {
                Db::rollback();
                return json(['code' => 100, 'msg' => '失败']);
            }
        }
        $order = Db::table('up_order')
            ->where('order_id',$order_id)
            ->find();
        $array = $this->arrayEmpty;
        $express2 = $order['order_nj_user2'] ? json_decode($order['order_nj_user2'],true) : $array;
        $express3 = $order['order_nj_user3'] ? json_decode($order['order_nj_user3'],true) : $array;
        return $this->fetch('order/ordersend',['type'=>$type,'data'=>$order,'express2'=>$express2,'express3'=>$express3]);
    }

    /**
     * 结束订单
     * @return \think\response\Json
     */
    public function orderDone(){
        $order_id = input('id');
        $now_status = Db::table('up_order')->where('order_id',$order_id)->value('order_status');
        if($now_status != 6) return json(['code' => 100, 'msg' => '只有已回寄的才可以操作']);
        $res = Db::table('up_order')
            ->where('order_id',$order_id)
            ->update([
                'order_status' => 7,
                'order_user_shou_time' => date('Y-m-d H:i:s')
            ]);
        if($res){
            return json(['code' => 200, 'msg' => '确认成功']);
        }else{
            return json(['code' => 100, 'msg' => '确认失败']);
        }
    }
    /**
     * 删除订单
     * @return \think\response\Json
     */
    public function orderDel(){
        $order_id = input('id');
//        $now_status = Db::table('up_order')->where('order_id',$order_id)->value('order_status');
//        if($now_status != 8) return json(['code' => 100, 'msg' => '只有已取消的订单才可以删除']);
        $res = Db::table('up_order')
            ->where('order_id',$order_id)
            ->update([
                'order_delete' => 2
            ]);
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
    /**
     * 查报修资料
     * @return mixed|\think\response\Json
     */
    public function orderDevice(){
        if(request()->isAjax()){
            extract(input());
            $order_id = input('order_id');
            //
            $where = [
                'oi_order_id' => $order_id
            ];
            $field = 'od.*,xl1.device_name xlname,xl2.device_name xhname,gz_name';
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 30;
            $count = Db::table('up_order_info')->alias('od')
                ->join("up_xl xl1",'od.oi_xl=xl1.device_id','LEFT')
                ->join("up_xl xl2",'od.oi_xl=xl2.device_id','LEFT')
                ->join("up_gz gz",'od.oi_gz=gz.gz_id','LEFT')
                ->where($where)
                ->count();// 获取总条数
            $details = Db::table('up_order_info')->alias('od')
                ->join("up_xl xl1",'od.oi_xl=xl1.device_id','LEFT')
                ->join("up_xl xl2",'od.oi_xl=xl2.device_id','LEFT')
                ->join("up_gz gz",'od.oi_gz=gz.gz_id','LEFT')
                ->where($where)
                ->page($Nowpage,$limits)
                ->field($field)
                ->select();
            foreach ($details as $k => $v){
                $details[$k]['imgs'] = [];
                if($v['oi_front_image']) $details[$k]['imgs'][] = $v['oi_front_image'];
                if($v['oi_side_image']) $details[$k]['imgs'][] = $v['oi_side_image'];
                if($v['oi_mp_image']) $details[$k]['imgs'][] = $v['oi_mp_image'];

                if($v['oi_other_image']){
                    $details[$k]['oi_other_image'] = explode(',',$v['oi_other_image']);
                }else{
                    $details[$k]['oi_other_image'] = [];
                }
                if(!$v['oi_other_video']) $details[$k]['oi_other_video'] = '';
            }

            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $details]);
        }
        $order_id = input('order_id');
        $this->assign('order_id',$order_id);
        return $this->fetch('order/devices');
    }
    /**
     * 订单发货
     */
    public function orderSonPdf(){
        extract(input());
        if(Request::instance()->isPost()){
            $update = [
                'oi_wx_report_name' => $oi_wx_report_name,
                'oi_wx_report_remark' => $oi_wx_report_remark,
                'oi_wx_report_url' => $oi_wx_report_url
            ];
            Db::table('up_order_info')
                ->where('oi_id',$oi_id)
                ->update($update);
            return json(['code' => 200, 'msg' => '成功']);
        }
        $order = Db::table('up_order_info')
            ->where('oi_id',$oi_id)
            ->find();
        return $this->fetch('order/infopdf',['data'=>$order]);
    }
    /**
     * 查看物流详情
     * @return mixed|\think\response\Json
     */
    public function wuliuInfo(){
        $code = input('express_no');
        $phone = input('phone');
        $oid = input('oid');
        $info = orderWuLiu($code,$phone);
        $info2 = [];
        $info3 = [];
        if($oid){
            $order = Db::table('up_order')
                ->where('order_id',$oid)
                ->find();
            $array = $this->arrayEmpty;
            $express2 = $order['order_nj_user2'] ? json_decode($order['order_nj_user2'],true) : $array;
            $express3 = $order['order_nj_user3'] ? json_decode($order['order_nj_user3'],true) : $array;
            $code2 = $express2['order_company_express_code'];
            if($code2) $info2 = orderWuLiu($code2,$express2['order_user_phone']);
            $code3 = $express3['order_company_express_code'];
            if($code3) $info3 = orderWuLiu($code3,$express3['order_user_phone']);
        }
//        dump($code);die;
        $this->assign('info',$info);
        $this->assign('info2',$info2);
        $this->assign('info3',$info3);
        return $this->fetch('order/wuliuinfo');
    }

    //导出
    public function excelOutOrder()
    {
        set_time_limit(0);//防止超时
        // ini_set("memory_limit", "256M");//防止内存溢出
        extract(input());
        $map = [];
        $map['o.order_delete'] = 1;
        $authSta = Db::table('up_auth_group')->where ('id' , session('agid'))->value ('auth_status');
        if($authSta == 2){
            $map['o.order_admin_id'] = session('uid');
        }
        if(isset($n_title)&&$n_title!="")
        {
            $map['order_username|order_phone|order_company'] = ['like',"%" . $n_title . "%"];
        }
        if(isset($order_no)&&$order_no!="")
        {
            $map['order_no'] = ['like',"%" . $order_no . "%"];
        }
        if(isset($order_status)&&$order_status!="")
        {
            $map['order_status'] = $order_status;
        }
        if(isset($order_jiance_status)&&$order_jiance_status!="")
        {
            $map['order_jiance_status'] = $order_jiance_status;
        }
        if(isset($start)&&$start!=""&&isset($end)&&$end=="")
        {
            $map['order_time'] = ['>= time',$start];
        }
        if(isset($end)&&$end!=""&&isset($start)&&$start=="")
        {
            $map['order_time'] = ['<= time',$end];
        }
        if(isset($start)&&$start!=""&&isset($end)&&$end!="")
        {
            $map['order_time'] = ['between time',[$start,$end]];
        }
        $field = [
            'order_id','order_no','order_company','oi_order_id','oi_xulie','order_wxfs_name', 'oi_wx_report_remark'
            , 'x1.device_name name1', 'x2.device_name name2', 'oi_desc', 'order_status'
        ];
        $data = Db::table('up_order')->alias('o')
            ->join('up_order_info od','o.order_id=od.oi_order_id','LEFT')
            ->where($map)
            ->count();
        if($data > 0){
            $cellname = [
                '订单ID','订单编号','现场', 'NO.', 'Track Label NO./Serial NO.', 'Customer return information', 'Debug Information', '系列', '型号', '故障描述', '订单状态'
            ];
			$status = ['order_status'];
            $values = [
                'order_status' => [1 => '待审核', 2 => '已同意(待寄回)', 3 => '已拒绝',4 => '返厂中(用户已寄回)', 5 => '检修中', 6 => '已回寄给用户', 7 => '已完成', 8 => '已取消']
            ];
            $res = exportExcelOrder('order',time(),$cellname,$data,$status,$values,1,ceil($data/10000),$map,$field);
            return json($res);
        }else{
            return json(['code'=>0,'msg'=>'此条件下无数据']);
        }
    }
    //导出
    public function excelOutDetail()
    {
        set_time_limit(0);//防止超时
        //ini_set("memory_limit", "512M");//防止内存溢出
        extract(input());
        $map = [];
        $map['o.is_delete'] = 0;
        $map['order_contract'] = ['<>',3];
        if(isset($n_title)&&$n_title!="")
        {
            $map['order_no|order_back_order_id|order_back_order_id|name|mobile|express_no'] = ['like',"%" . $n_title . "%"];
        }
        if(isset($contract)&&$contract!="")
        {
            $map['order_contract'] = $contract;
        }else{
            return json(['code'=>0,'msg'=>'请选择导出增补还是自营']);
        }
        if(isset($orderid)&&$orderid!="")
        {
            $map['o.order_id'] = $orderid;
        }
        if(isset($type) && $type){
            if($type == 1){
                $map['is_pay'] = 0;
            }elseif ($type == 6){
                $map['is_pay'] = 2;
            }else{
                $map['is_pay'] = 1;
                if($type == 2){
                    $map['is_send'] = 0;
                }
                if($type == 3){
                    $map['is_send'] = 1;
                }
                if($type == 4){
                    $map['is_send'] = 2;
                }
                if($type == 5){
                    $map['is_send'] = 3;
                }
            }
        }
        if(isset($oldnew)&&$oldnew!="")
        {
            $map['new_old'] = $oldnew;
        }
        if(isset($fapiao)&&$fapiao!="")
        {
            $map['fapiao_status'] = $fapiao;
        }
        if(isset($schoolid) && $schoolid != '' && $schoolid != null && $schoolid != 'undefined'){
            $map['s.school_erp'] = $schoolid;
        }
        if(isset($start)&&$start!=""&&isset($end)&&$end=="")
        {
            $map['pay_time'] = ['>= time',$start];
        }
        if(isset($end)&&$end!=""&&isset($start)&&$start=="")
        {
            $map['pay_time'] = ['<= time',$end];
        }
        if(isset($start)&&$start!=""&&isset($end)&&$end!="")
        {
            $map['pay_time'] = ['between time',[$start,$end]];
        }
        if(isset($push)&&$push!="")
        {
            $map['order_push_erp'] = $push;
        }
        if($contract == 1){
            $field = ['LEFT(pay_time,10)' => 'pay_date','o.order_no','good_name','good_coding','od.goods_info','num',
                'unit_price','num * unit_price as goodallmoney',
                'name','mobile','address','express_original_price','o.is_pay','o.is_send','order_back_order_id'
                ,'o.express','o.express_no','o.order_cancel_status','o.order_push_erp'
            ];
            $data = Db::table('up_self_order')->alias('o')
                ->join('up_self_order_detail od','o.order_id=od.order_id','LEFT')
                ->join('up_self_goods g','od.goods_id=g.good_id','LEFT')
                ->where($map)
                ->field($field)
                ->select();
            if($data){
                $cellname = [
                    '付款日期', '订单编号', '商品名字', '商品编码', '规格信息', '数量',
                    '单价', '总额',
                    '收件人', '收件电话', '收件地址','快递费', '支付状态', '订单状态', '付款流水'
                    ,'物流公司', '物流单号', '是否取消', '推送状态'
                ];
                $status = ['is_pay','is_send','order_cancel_status','order_push_erp'];
                $values = [
                    'is_pay' => [0 => '未付款',1 => '已付款',2 => '已取消']
                    ,'is_send' => [0 => '未发货',1 => '已发货待收货',2 => '已收货待评价',3 => '已完成']
                    ,'order_cancel_status' => [1 => '正常单',2 => '取消被拒',3 => '已同意取消',4 => '取消待审核']
                    , 'order_push_erp' => [1 => '未推送',2 => '已推送',3 => '已推送']
                ];
                $res = exportExcel('selfOrderDatas',time(),$cellname,$data,$status,$values);
                return json($res);
            }else{
                return json(['code'=>0,'msg'=>'此条件下无数据']);
            }
        }
        $field = ['LEFT(pay_time,10)' => 'pay_date','o.order_no','o.order_school','school_name','uson_year',
            'uson_grade','uson_class','uson_username','user_gender','goods_coding_unique goods_coding_unique_less',
            'good_pinming as pm','attr_id','num','unit_price','num * unit_price as goodallmoney',
            'name','mobile','address','express_original_price','o.is_pay','o.is_send','order_back_order_id'
            ,'o.express','o.express_no','o.order_cancel_status',
            'g.good_pinming','o.tixing','o.shengao','o.tizhong','o.xwei','o.ywei','o.twei','o.jiankuan','o.xiuchang','o.order_push_erp'
        ];
        $data = Db::table('up_self_order')->alias('o')
            ->join('up_self_order_detail od','o.order_id=od.order_id','LEFT')
            ->join('up_goods g','od.goods_id=g.good_id','LEFT')
            ->join('up_u_user_son u','o.order_user_son_id=u.uson_id','LEFT')
            ->join('up_school s','o.order_school=s.school_erp','LEFT')
            ->where($map)
            ->count();
        if($data > 0){
            $cellname = [
                '付款日期', '订单编号', '学校ID', '学校名字', '入学年份',
                '年级','班级', '姓名', '性别', '款号',
                '品名', '尺码', '数量', '单价', '总额',
                '收件人', '收件电话', '收件地址','快递费', '支付状态', '订单状态', '付款流水'
                ,'物流公司', '物流单号', '是否取消',
                '衣服类型','体型', '身高', '体重', '胸围', '腰围', '臀围','肩宽','袖长','推送状态'
            ];
//            $status = ['is_pay','pay_type','is_send','order_contract','order_cancel_status','order_cancel_pay','new_old','th_status'];
//            $values = [
//                 'is_pay' => [0 => '待支付',1 => '已支付',2 => '已取消']
//                ,'pay_type' => [1 => '微信',2 => '支付宝',3 => '银联']
//                ,'is_send' => [0 => '未发货',1 => '已发货待收货',2 => '已收货待评价',3 => '已完成']
//                ,'order_contract' => [1 => '自营订单',2 => '增补订单']
//                ,'order_cancel_status' => [1 => '未取消',2 => '取消被拒',3 => '已同意取消',4 => '待审核']
//                ,'order_cancel_pay' => [1 => '正常',2 => '未打款',3 => '已打款']
//                ,'new_old' => [1 => '新APP订单',2 => '旧APP订单']
//                ,'th_status' => [0 => '无售后',1 => '换货',2 => '退货']
//            ];
            $status = ['tixing','is_pay','user_gender','is_send','order_cancel_status','order_push_erp'];
            $values = [
                'tixing' => [1 => '正常',2 => '特殊体型'],
                'is_pay' => [0 => '未付款',1 => '已付款',2 => '已取消'],
                 'user_gender' => [1 => '男',2 => '女']
                ,'is_send' => [0 => '未发货',1 => '已发货待收货',2 => '已收货待评价',3 => '已完成']
                ,'order_cancel_status' => [1 => '正常单',2 => '取消被拒',3 => '已同意取消',4 => '取消待审核'],
                'order_push_erp' => [1 => '未推送',2 => '已推送',3 => '已推送']
            ];
            $res = exportExcelOrder('orderinfo',time(),$cellname,$data,$status,$values,2,ceil($data/10000),$map,$field);
            return json($res);
        }else{
            return json(['code'=>0,'msg'=>'此条件下无数据']);
        }
    }
}